<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');

function SaveNewSGF() {
	$ks_num = $_REQUEST['ks_num'];
	$ks_sgf = $_REQUEST['ks_sgf'];

	$db = OpenDB();
	$query = "select ks_sgf from key_sgf where ks_num = $ks_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   Error("Schl&uuml;ssel $ks_num ist bereits vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "insert into key_sgf (ks_num, ks_sgf) values ($ks_num, '$ks_sgf')";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

function SaveEditSGF() {
	$ks_num = $_REQUEST['ks_num'];
	$ks_sgf = $_REQUEST['ks_sgf'];

	$db = OpenDB();
	$query = "select ks_sgf from key_sgf where ks_num = $ks_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error("Schl&uuml;ssel $ks_num ist nicht vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "update key_sgf set ks_sgf = '$ks_sgf' where ks_num = $ks_num";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

$newsgf = $_REQUEST['newsgf'];
$editsgf = $_REQUEST['editsgf'];

if (!isset($editsgf)) {
   $editsgf = 0;
}

$savenewsgf = $_REQUEST['savenewsgf'];
$saveeditsgf = $_REQUEST['saveeditsgf'];

if (isset($savenewsgf)) {
   SaveNewSGF();
}

if (isset($saveeditsgf)) {
   SaveEditSGF();
}

?>
<form action="sgf.php" method="post">
<input type="hidden" name="headline" value="15">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<button type="submit" name="newsgf" value="newsgf"><img src="image/button_newsgf.png" border=0 alt="Neuer SGF"></button>
<?php
$db = OpenDB();
$query = "select ks_num, ks_sgf from key_sgf order by ks_num";
$result = QueryDB($db, $query);

if (!$result) {
   echo "</table></form><br>\n";
   Error("SQL-Error: $query");
   require('footer.inc');
   exit;
}

$numrows = numrowsDB($result);

if ($numrows > 0 || isset($newsgf)) {
?>
<table border=1>
   <tr>
      <td class="tbhead">&nbsp;</td>
      <td class="tbhead">Key</td>
      <td class="tbhead">Text</td>
   </tr>
<?php
   $row = 0;

   while ($row < $numrows) {
      $data = fetchDB($result, $row);
      $ks_num = $data[0];
      $ks_sgf = $data[1];

      if ($ks_num == $editsgf) {
         echo "<input type=\"hidden\" name=\"ks_num\" value=\"$ks_num\">\n";
         echo "<tr><td><button type=\"submit\" name=\"saveeditsgf\" value=\"$ks_num\"><img src=\"image/new.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$ks_num</td>";
         echo "<td><input type=\"text\" name=\"ks_sgf\" value=\"$ks_sgf\" size=20 maxlength=20></td></tr>\n";
      } else {
         echo "<tr><td><button type=\"submit\" name=\"editsgf\" value=\"$ks_num\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$ks_num</td><td>$ks_sgf</td></tr>";
      }

      $row++;
   }

   if (isset($newsgf)) {
      echo "<tr><td><button type=\"submit\" name=\"savenewsgf\" value=\"savenewsgf\"><img src=\"image/new.png\" border=0 alt=\"Neu\"></button></td>";
      echo "<td><input type=\"text\" name=\"ks_num\" size=7 maxlength=7></td>";
      echo "<td><input type=\"text\" name=\"ks_sgf\" size=20 maxlength=20></td></tr>\n";
   }
?>
</table>
<button type="submit" name="newsgf" value="new"><img src="image/button_newsgf.png" border=0 alt="Neuer SGF"></button>
</form>
<?php
}

require('footer.inc');
?>
